/*
* @Author: Josh
* @Date:   2019-01-29 18:03:57
* @Last Modified by:   Administrator
* @Last Modified time: 2019-04-27 21:38:54
*/
'use strict';

require('page/common/header/index.js');
require('./index.css');
var nav           = require('page/common/nav/index.js'),
    _mall         = require('util/mall.js'),
    _cart         = require('service/cart-service.js'),
    templateIndex = require('./index.string');

var page = {
    data : {
        
    },
    init : function(){
        this.onLoad();
        this.bindEvent();
    },
    onLoad : function(){
        this.loadCart();
    },
    bindEvent : function(){
        var _this = this;
        //商品的选中
        $(document).on('click', '.cart-select', function(){
            var $this     = $(this),
                //取得渲染在cart-table上的product-id
                productId = $this.parents('.cart-table').data('product-id');
            // 选中
            if($this.is(':checked')){
                _cart.selectProduct(productId, function(res){
                    _this.renderCart(res);
                }, function(errMsg){
                    _this.showCartError();
                });
            }
            //取消选中
            else{
                 _cart.unselectProduct(productId, function(res){
                    _this.renderCart(res);
                }, function(errMsg){
                    _this.showCartError();
                });
            }
        });
         //商品的全选
        $(document).on('click', '.cart-select-all', function(){
            var $this = $(this);
            // 全选
            if($this.is(':checked')){
                _cart.selectAllProduct(function(res){
                    _this.renderCart(res);
                }, function(errMsg){
                    _this.showCartError();
                });
            }
            //取消全选
            else{
                 _cart.unselectAllProduct(function(res){
                    _this.renderCart(res);
                }, function(errMsg){
                    _this.showCartError();
                });
            }
        });
        //商品数量
        $(document).on('click', '.count-btn', function(){
            var $this     = $(this),
                $pCount   = $this.siblings('.count-input'),
                currCount = parseInt($pCount.val()),
                type      = $this.hasClass('plus') ? 'plus' : 'minus',
                productId = $this.parents('.cart-table').data('product-id'),
                minCount  = 1,
                maxCount  = parseInt($pCount.data('max')),
                newCount  = 0;

            if(type === 'plus'){
                if(currCount >= maxCount){
                    _mall.errorTips('数量达到上限！');
                    return;
                }
                newCount = currCount + 1;
            }
            else if(type === 'minus'){
                if(currCount <= minCount){
                    return;
                }
                newCount = currCount - 1;
            }
            //更新购物车商品数量
            _cart.updateProduct({
                productId : productId,
                count : newCount
            }, function(res){
                _this.renderCart(res);
            }, function(errMsg){
                _this.showCartError();
            }); 
        });
        //删除单个商品
        $(document).on('click', '.cart-delete', function(){
            if(window.confirm('确定要删除该商品吗？')){
                var productId = $(this).parents('.cart-table')
                    .data('product-id');
                _this.deleteCartProduct(productId);
            }
        });
        //删除选中商品
        $(document).on('click', '.delete-selected', function(){
            if(window.confirm('确定要删除选中的商品吗？')){
                var arrProductIds = [],
                    $selectedItem = $('.cart-select:checked'),
                    iLength       = $selectedItem.length;
                for(var i = 0; i < iLength; i++){
                    //循环查找选中的productIds
                    arrProductIds.push($($selectedItem[i])
                        .parents('.cart-table').data('product-id'));
                }
                if(arrProductIds.length){
                    //join 用连接符 将数组元素拼接成字符串
                    _this.deleteCartProduct(arrProductIds.join(','));
                }
                else{
                    _mall.errorTips('你还没有选中商品鸭！');
                }
            }
        });
        //下单
        $(document).on('click', '.btn-submit', function(){
            //总价大于0，进行提交
            if(_this.data.cartInfo && _this.data.cartInfo.cartTotalPrice > 0){
                window.location.href = './order-confirm.html';
            } 
            else{
                _mall.errorTips('你还没有选择商品鸭！');
            }
        });
    },
    //加载购物车信息
    loadCart : function(){
       var _this     = this;
       //获取购物车信息
       _cart.getCartList(function(res){
            _this.renderCart(res);
       }, function(errMsg){
            _this.showCartError();
       });   
    },
    //渲染购物车
    renderCart : function(data){
        //判断购物车是否为空,用于模板渲染是判断是否显示购物车为空的提示
        this.filter(data);
        //缓存购物车信息
        this.data.cartInfo = data;
        //生成html
        var cartHtml = _mall.renderHtml(templateIndex, data);
        $('.page-wrap').html(cartHtml);
        // 通知导航栏购物车刷新数量
        nav.loadCartCount();
    },
    //删除指定商品,支持批量，productId用逗号分隔
    deleteCartProduct : function(productIds){
        var _this = this;
        _cart.deleteProduct(productIds, function(res){
            _this.renderCart(res);
        }, function(errMsg){
            _this.showCartError();
        });
    },
    //数据匹配
    filter : function(data){
        data.notEmpty = !!data.cartProductVoList.length; 
    },
    //显示错误信息
    showCartError : function(){
        $('.page-wrap').html('<p class="err-tip">加载不出来？刷新下看看？</p>');
    }
};

$(function(){
    page.init();
})